package model

import (
	"gorm.io/gorm"
	"time"
)

type Chats struct {
	Id        uint64         `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	UserId    uint64         `gorm:"column:user_id;type:bigint(20) UNSIGNED;comment:用户编号;not null;" json:"user_id"`     // 用户编号
	DoctorId  uint64         `gorm:"column:doctor_id;type:bigint(20) UNSIGNED;comment:医生编号;not null;" json:"doctor_id"` // 医生编号
	ImagesUrl string         `gorm:"column:images_url;type:varchar(255);comment:图片url;not null;" json:"images_url"`     // 图片url
	Mesage    string         `gorm:"column:mesage;type:varchar(30);comment:信息;not null;" json:"mesage"`                 // 信息
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (c *Chats) TableName() string {
	return "chats"
}
func CreateAnimals(db *gorm.DB, cc *Chat, ccs *Chats) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&cc).Error; err != nil {
		tx.Rollback()
		return err
	}

	if err := tx.Create(&ccs).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
